/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package mg.daloji.antandroyd.ui;

import mg.daloji.antandroyd.CONSTANTS;
import mg.daloji.antandroyd.R;
import mg.daloji.antandroyd.Update;
import mg.daloji.antandroyd.application.App;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;



/**
 *
 * @author Johan Boris Iantila
 *
 */

public class Bootstrap extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		CheckConfig();
	}


	private  void CheckConfig(){
		if(!haveNetworkConnection()){
			showError();
		}else{
			checkVersion();
		}
	
	}
	

	
	private void checkVersion(){
		SharedPreferences sharedPreferences = getSharedPreferences(CONSTANTS.PREFS_NAME, MODE_PRIVATE);
		boolean firstInstall = sharedPreferences.getBoolean(CONSTANTS.FIRST_INSTALL,true);
		if(firstInstall){
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putBoolean(CONSTANTS.FIRST_INSTALL,false);
			Update upt=App.getupdate();
			Log.i("tag", " =>" +upt.getVersion());
			Log.i("tag", " =>" +App.getupdate().getMajorVersion());
			Log.i("tag", " =>" +App.getupdate().getMinorVersion());
			
			editor.putInt(CONSTANTS.VERSION,  App.getupdate().getVersion());
			editor.putInt(CONSTANTS.MAJOR,  App.getupdate().getMajorVersion());
			editor.putInt(CONSTANTS.MINOR,  App.getupdate().getMinorVersion());
			editor.commit();
		}else{
			int version = sharedPreferences.getInt(CONSTANTS.VERSION,-1);
			int major = sharedPreferences.getInt(CONSTANTS.MAJOR,-1);
			int minor = sharedPreferences.getInt(CONSTANTS.MINOR,-1);
			if(App.getupdate().getVersion()>version || App.getupdate().getMajorVersion()>major ||App.getupdate().getMinorVersion()>minor){
				showError();
				
			}
		}
		
	}

	private boolean haveNetworkConnection() {
		boolean haveConnectedWifi = false;
		boolean haveConnectedMobile = false;

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] netInfo = cm.getAllNetworkInfo();
		for (NetworkInfo ni : netInfo) {
			if (ni.getTypeName().equalsIgnoreCase("WIFI"))
				if (ni.isConnected())
					haveConnectedWifi = true;
			if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
				if (ni.isConnected())
					haveConnectedMobile = true;
		}
		return haveConnectedWifi || haveConnectedMobile;
	}

	private void showError() {
		final AlertDialog.Builder builder =  new AlertDialog.Builder(this)
		.setTitle(R.string.app_name) 
		.setMessage(R.string.service_error_network)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setCancelable(false);

		builder.setPositiveButton(R.string.service_positive_error_network,new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finish();
			} }); 
		builder.show();

	}

}
